package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
	"strconv"
	"strings"
)

func main() {
	fmt.Println(
		//101
		convertToBin(5),
		//1101
		convertToBin(11),
	)

	printFile("go-new/go-01-basic/test.txt")
	s := `begin "abb"
	456
	'跨行字符串'
	使用反引号`
	printFileContents(strings.NewReader(s))
}

func convertToBin(n int) string {
	result := ""
	for ; n > 0; n /= 2 {
		lsb := n % 2
		result = strconv.Itoa(lsb) + result
	}
	return result
}

func printFile(filename string) {
	file, err := os.Open(filename)
	if err != nil {
		//出现异常,终止程序继续向下执行
		panic(err)
	}
	printFileContents(file)
}

func printFileContents(reader io.Reader) {
	scanner := bufio.NewScanner(reader)
	//只有结束条件情况, 没有初始化和递增条件
	for scanner.Scan() {
		fmt.Println(scanner.Text())
	}
}
